import Vue from 'vue'
import App from './App.vue'
import router from './router'
/*import store from './store'*/
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import locale from 'iview/dist/locale/zh-CN';
import qs from 'qs'
import common from '../public/js/common'
import Axios from 'axios'
import Vant from  'vant'
import 'vant/lib/index.css'

Vue.prototype.$http = Axios
Vue.prototype.$qs = qs
Vue.prototype.common = common
Vue.config.productionTip = false
Vue.use(iView, {locale});
Vue.use(Vant);

var vue = new Vue({
    router,
    /* store,*/
    render: h => h(App)
}).$mount('#app')

//Axios.defaults.baseURL ='http://eternal.nat100.top';
Axios.interceptors.request.use(function (config) {
    var self = vue;
    // 在发送请求之前做些什么
    if (self.common.getStorageS('userInfo')) {  // 每次发送请求之前判断是否存在token，如果存在，则统一在http请求的header都加上token，不用每次请求都手动添加了
        config.headers.Authorization = self.common.getStorageS('userInfo');
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


// 添加响应拦截器
Axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
}, function (error) {
    // 对响应错误做点什么
    var self = vue;
    switch (error.response.status) {
        case 401:
            if (error.response.data.code == undefined) {
                self.$Message.info({
                    content: '用户未登陆，请登陆系统！',
                    duration: 5
                });

                setTimeout(function (){
                    self.common.removeStorageS("userInfo");
                    self.$router.push({path: '/'});
                },3000);
            } else if("100004"===error.response.data.code){
                self.$Message.info({
                    content: error.response.data.message,
                    duration: 5
                });
                setTimeout(function (){
                    self.common.removeStorageS("userInfo");
                    self.$router.push({path: '/'});
                },3000);
            } else {

                return Promise.reject(error);
            }
            break;
        case 403:
            var msg = error.response.data.message;
            if (undefined == msg) {
                msg = '';
            }
            self.$Message.info({
                content: '用户没有该权限，无法操作！<br/>' + msg,
                duration: 5
            });
            break;
        case 500:
            if (error.response.data.code == undefined) {
                self.$Message.info({
                    content: '500 服务器错误：' + error.response.data.message,
                    duration: 5
                });
            } else {
                return Promise.reject(error);
            }
            break;
        default:
            return Promise.reject(error);
    }

});


